package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.model.User;
import com.example.forum.services.IArticleService;
import com.example.forum.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.coyote.http11.HttpOutputBuffer;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "文章接口")
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private IArticleService articleService;
    @Resource
    private IBoardService boardService;

    /**
     * 新增文章的http接口
     * @param request 用来获取此时登录的用户
     * @param boardId 文章的板块
     * @param title 文章的标题
     * @param content 文章的内容
     * @return 返回成功或者失败
     */
    @ApiOperation("发布新贴")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("板块ID")@RequestParam("boardId")@NonNull Long boardId,
                            @ApiParam("文章标题") @RequestParam("title") @NonNull String title,
                            @ApiParam("文章内容") @RequestParam("content") @NonNull String content){

        //响应
        return  articleService.create(request,boardId,title,content);
    }
    @ApiOperation("获取帖子列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>>getAllByBoardId(@ApiParam("板块ID")@RequestParam(value = "boardId",required = false)Long boardId){
        //定义返回的集合
        Long boarId=boardId;
        List<Article>articles;
        articles=articleService.selectAllByBoardId(boarId);
        //如果结果为空
        if(articles==null){
            articles=new ArrayList<>();
        }
        //返回响应结果
        return AppResult.success(articles);
    }
    @ApiOperation("根据帖子Id获取详情")
    @GetMapping("/details")
    public AppResult details(HttpServletRequest request,
                                     @ApiParam("帖子Id")@RequestParam("id")@NonNull Long id){
        //获取当前登录的用户信息
        HttpSession session=request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        //检验用户状态
        if (user.getState() == 1) {
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //调用service获取帖子详情
        Article article=articleService.selectDetailById(id);
        //判断结果是否为空
        if(article==null){
            //返回错误信息
            return  AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断当前用户是否为作者
        if(user.getId()==article.getUserId()){
            article.setOwn(true);
        }
        //返回结果
        return AppResult.success(article);
    }
    @ApiOperation("修改帖子")
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,
                            @ApiParam("帖子ID")@RequestParam("id")@NonNull Long id,
                            @ApiParam("帖子标题")@RequestParam("title")@NonNull String title,
                            @ApiParam("帖子正文")@RequestParam("content")@NonNull String content){
        //获取当前登录的用户信息
        HttpSession session=request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        //检验用户的状态
        if(user.getState()==1){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            //抛出异常
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查找帖子详情
        Article article=articleService.selectById(id);
        // 判断用户是不是作者
        if (user.getId() != article.getUserId()) {
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        // 判断帖子的状态 - 已归档
        if (article.getState() == 1 || article.getDeleteState() == 1) {
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //调用service
        articleService.modify(id,title,content);
        log.info("帖子更新成功+id="+id);
        //返回正确的结果
        return AppResult.success();
    }
    @ApiOperation("点赞")
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,@ApiParam("帖子ID")@RequestParam("id")@NonNull Long id){
        HttpSession session=request.getSession(false);
        //检验用户状态查看用户是否被禁言
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            // 返回结果
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 调用Service
        articleService.thumbsUpById(id);
        // 返回结果
        return AppResult.success();
    }
    @ApiOperation("删除帖子")
    @PostMapping("/delete")
    public AppResult deleteById(HttpServletRequest request,
                                @ApiParam("帖子ID")@RequestParam("id")@NonNull Long id){
        HttpSession session=request.getSession(false);
        User user=(User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            //该用户已经被禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子详情
        Article article=articleService.selectById(id);
        //查看登录用户是否是该帖子的作者
        if(user.getId()!=article.getUserId()){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        //检验帖子状态
        if(article==null||article.getDeleteState()==1){
            //帖子已经被删除
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //调用service
        articleService.deleteById(id);
        return AppResult.success();
    }
    @ApiOperation("获取用户帖子列表")
    @GetMapping("/getAllByUserId")
    public AppResult getAllByUserId(HttpServletRequest request,
                                    @ApiParam("用户ID")@RequestParam(value = "userId",required = false) Long userId){
        if(userId==null){
            HttpSession session=request.getSession(false);
            User user=(User) session.getAttribute(AppConfig.USER_SESSION);
            userId=user.getId();
        }
        //调用Service
        List<Article>articles=articleService.selectByUserId(userId);
        return AppResult.success(articles);
    }
}
